#region (c) 2014 THN Solutions LLC. - All rights reserved
/*
THN MVC MODULE FOR THN WEB SERVER

Copyright (c) 2014, THN Solutions LLC. ( www.ThnSolutions.com )
Author: Nguyễn, M. Hải                 ( www.Minh-Hai.com     )
All rights reserved.

This library is dual-licensed.
  + For use in commercial software, please obtain a commercial license from THN Solutions LLC.    

  + For use in free software, this library is licensed under GPL version 3. A summary of 
	GPLv3 is listed below. You should also find a copy of GPLv3 as file License_GPLv3.txt 
	included with the source-code files.

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License version 3 as published by
    the Free Software Foundation  of the License.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#endregion

#region Record of Change
//             							CHANGE HISTORY
// -------------------------------------------------------------------------------------
// |   DATE    | DEVELOPER  | DESCRIPTION                                              |
// -------------------------------------------------------------------------------------
// | 14-Apr-13 | HaiNM      | First creation.                                          |
// -------------------------------------------------------------------------------------
//
#endregion

#region Record of Audit
//             							REVIEW HISTORY
// -------------------------------------------------------------------------------------
// |   DATE    | REVIEWER   | NOTES                                                    |
// -------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------
//
#endregion

#region Using directives
using System;

using Thn;
using Thn.IO;
using Thn.Net.Http;
#endregion

namespace Thn.Web.Mvc
{
    /// <summary>
    /// Base class for view template used in rendering views.
    /// </summary>
#if DEBUG || TESTING
    public
#else
    public
#endif
 class ViewTemplate : IViewTemplate
    {
        #region Fields
        #endregion

        #region Properties

        #endregion

        #region IViewTemplate Members

        #region Model
        private object mModel;
        /// <summary>
        /// Gets or sets the data model.
        /// </summary>
        public object Model
        {
            get { return mModel; }
            set { mModel = value; }
        }
        #endregion

        #region Writer
        private IContextWriter mWriter;
        /// <summary>
        /// Gets or sets the writer responsible for writing outputs.
        /// </summary>
        public IContextWriter Writer
        {
            get { return mWriter; }
            set { mWriter = value; }
        }
        #endregion

        #region Context
        private IHttpRequestHandlingContext mContext;
        /// <summary>
        /// Gets or sets the current request handling context.
        /// </summary>
        public IHttpRequestHandlingContext Context
        {
            get { return mContext; }
            set { mContext = value; }
        }
        #endregion

        #region Request
        /// <summary>
        /// Gets reference to the request context.
        /// </summary>
        public HttpRequest Request
        {
            get { return mContext != null ? (HttpRequest)mContext.Request : null; }
        }
        #endregion

        #region Execute
        /// <summary>
        /// Razor Parser overrides this method
        /// </summary>
        public virtual void Execute() { }
        #endregion

        #region Renderer
        private IViewRenderer mRenderer;
        /// <summary>
        /// Gets or sets the renderer responsible for rendering views.
        /// </summary>
        public IViewRenderer Renderer
        {
            get { return mRenderer; }
            set { mRenderer = value; }
        }
        #endregion

        #endregion

        #region Write - object
        public virtual void Write(object value)
        {
            //Response.Write(value);

            if (value != null) mWriter.Write(value.ToString(), StringMode.Exact);
        }
        #endregion

        #region Write - string
        public virtual void WriteString(object value)
        {
            //Response.Write(value.ToString() + "\r\n");

            if (value != null) mWriter.WriteLine(value.ToString());
        }
        #endregion

        #region Write Literal
        public virtual void WriteLiteral(object value)
        {
            //Response.Write(value);

            if (value != null) mWriter.Write(value.ToString(), StringMode.Exact);
        }
        #endregion

        #region Render View
        ///// <summary>
        ///// Render a view template with the provided data model.
        ///// </summary>
        ///// <param name="viewName">Name of the view to be rendered.</param>
        ///// <param name="model">The data model object to be used by the view.</param>
        //public void RenderCustomView(string viewName)
        //{
        //    RenderArgs args = new RenderArgs();
        //    args.ViewName = viewName;
        //    //args.Model = model != null ? model : mModel;
        //    args.Writer = mWriter;
        //    mRenderer.Render(args);
        //}
        #endregion

        #region Constructors
        /// <summary>
        /// Create a new instance.
        /// </summary>
        public ViewTemplate()
            : base()
        { }
        #endregion
    }
}
